#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
    Date  :     2021/12/18 14:49
    Desc  :
--------------------------------------
"""
from django_http_log.serializers import DynamicFieldsModelSerializer
from rest_framework import serializers

from common.enums.person_enums import MenuTypeEnum
from common.utils.log_utils import logger
from person.models import Menu


class MenuSerializer(DynamicFieldsModelSerializer):
    parentCodeName = serializers.SerializerMethodField()

    class Meta:
        model = Menu
        fields = '__all__'

    def get_parentCodeName(self, obj):
        if obj.parentCode:
            queryset = self.Meta.model.objects.filter(id = int(obj.parentCode.id), deleted = False)
            return queryset.first().name
        return None


class MenuChildrenSerializer(DynamicFieldsModelSerializer):
    children = MenuSerializer(many = True, read_only = True, exclude = ['deleted', 'deleteTime'])
    parentCodeName = serializers.SerializerMethodField()

    class Meta:
        model = Menu
        fields = '__all__'

    def get_parentCodeName(self, obj):
        if obj.parentCode:
            queryset = self.Meta.model.objects.filter(id = int(obj.parentCode.id), deleted = False)
            return queryset.first().name
        return None


class MenuArraySerializer(DynamicFieldsModelSerializer):
    children = MenuChildrenSerializer(many = True, read_only = True, exclude = ['deleted', 'deleteTime'])
    parentCodeName = serializers.SerializerMethodField()

    class Meta:
        model = Menu
        fields = '__all__'
        deepth = 5

    def get_parentCodeName(self, obj):
        if obj.parentCode:
            queryset = self.Meta.model.objects.filter(id = obj.parentCode.id, deleted = False)
            return queryset.first()
        return None


class MenuParentCodeListSerializer(DynamicFieldsModelSerializer):
    children = serializers.SerializerMethodField()

    class Meta:
        model = Menu
        fields = '__all__'

    def get_children(self, obj):
        if obj:
            queryset = self.Meta.model.objects.filter(parentCode = obj.id,
                                                      deleted = False)
            return MenuSerializer(queryset, many = True, read_only = True, exclude = ['deleted', 'deleteTime']).data

        return []


class MenuTreeSerializer(DynamicFieldsModelSerializer):
    children = serializers.SerializerMethodField()

    class Meta:
        model = Menu
        fields = '__all__'

    def get_children(self, obj):
        if obj:
            queryset = self.Meta.model.objects.filter(type != MenuTypeEnum.BUTTON,
                                                      deleted = False)
            return MenuSerializer(queryset, many = True, read_only = True, exclude = ['deleted', 'deleteTime']).data

        return []
